using Godot;
using System;

public partial class EndScreen : CanvasLayer
{
	Button continueButton;
	Button quitButton;

	PanelContainer panelContainer;
	ScreenTransition screenTransition;

	// Called when the node enters the scene tree for the first time.
	public override void _Ready()
	{
		screenTransition = GetNode<ScreenTransition>("/root/ScreenTransition");
		continueButton = GetNode<Button>("%ContinueButton");
		quitButton = GetNode<Button>("%QuitButton");
		panelContainer = GetNode<PanelContainer>("%PanelContainer");
		panelContainer.PivotOffset = panelContainer.Size/2;
		Tween tween = CreateTween();
		tween.TweenProperty(panelContainer,"scale",Vector2.Zero,0);
		tween.TweenProperty(panelContainer,"scale",Vector2.One,.3)
		.SetEase(Tween.EaseType.Out).SetTrans(Tween.TransitionType.Back);

		GetTree().Paused = true;
		continueButton.Pressed += OnContinueButtonPressed;
		quitButton.Pressed += OnQuitButtonPressed;
	}

	// Called every frame. 'delta' is the elapsed time since the previous frame.
	public override void _Process(double delta)
	{
	}

	public void SetDefeat(){
		GetNode<Label>("%TitleLabel").Text = "Defeat";
		GetNode<Label>("%DescriptionLabel").Text = "You lost!";
		this.PlayJingle(true);
	}

	public void PlayJingle(bool defeat = false){
		if(defeat){
			GetNode<AudioStreamPlayer>("DefeatStreamPlayer").Play();
		}else{
			GetNode<AudioStreamPlayer>("VictoryStreamPlayer").Play();
		}
	}

	public async void OnContinueButtonPressed(){
		screenTransition.Transition();
		await ToSignal(screenTransition,nameof(screenTransition.TransitionedHalfway)); //等待信号结束
		GetTree().Paused = false;
		GetTree().ChangeSceneToFile("res://scenes/ui/meta_menu.tscn");
		// GetTree().ReloadCurrentScene();
	}

	public async void OnQuitButtonPressed(){
		screenTransition.TransitionToScene("res://scenes/main/main.tscn");
		await ToSignal(screenTransition,nameof(screenTransition.TransitionedHalfway)); //等待信号结束
		GetTree().Paused = false;
	}
}
